/*
 * Copyright (C) 2015 Miquel Sas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package traderlib.core.data.db;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import traderlib.core.data.entity.FieldList;
import traderlib.core.data.types.ByteArray;
import traderlib.core.data.types.Date;
import traderlib.core.data.types.KeyPointer;
import traderlib.core.data.types.OrderKey;
import traderlib.core.data.types.Time;
import traderlib.core.data.types.Timestamp;
import traderlib.core.data.types.Value;

/**
 * A record packs a list of values and their corresponding column definitions.
 *
 * @author Miquel Sas
 */
public class Record implements Comparable<Object> {

	/**
	 * The list of values.
	 */
	private List<Value> values;
	/**
	 * The list of columns.
	 */
	private FieldList columns;

	/**
	 * Default constructor.
	 */
	public Record() {
		super();
	}

	/**
	 * Sets the column list.
	 *
	 * @param columns The column list.
	 */
	public void setColumnList(FieldList columns) {
		this.columns = columns;
	}

	/**
	 * Returns the number of columns.
	 *
	 * @return The number of columns.
	 */
	public int getColumnCount() {
		return columns.size();
	}

	/**
	 * Returns the columns, for use in the friend class Cursor.
	 *
	 * @return The column list.
	 */
	public FieldList getColumnList() {
		return columns;
	}

	/**
	 * Get the column at the given index.
	 *
	 * @param index The indx of the column.
	 * @return The column.
	 */
	public Column getColumn(int index) {
		return (Column) columns.getField(index);
	}

	/**
	 * Get a column by alias.
	 *
	 * @param alias The column alias.
	 * @return The column or null if not found.
	 */
	public Column getColumn(String alias) {
		return (Column) columns.getField(alias);
	}

	/**
	 * Clears this record columns to their default values.
	 */
	public void clear() {
		values.clear();
		for (int i = 0; i < getColumnCount(); i++) {
			values.add(getColumn(i).getDefaultValue());
		}
	}

	/**
	 * Returns the value at a given index.
	 *
	 * @param index The index
	 * @return The value at the given index.
	 */
	public Value getValue(int index) {
		return values.get(index);
	}

	/**
	 * Get a value by column alias.
	 *
	 * @param alias The column alias
	 * @return The value.
	 */
	public Value getValue(String alias) {
		int index = columns.getFieldIndex(alias);
		return (index == -1 ? null : getValue(index));
	}

	/**
	 * Returns the list of values.
	 *
	 * @return The list of values.
	 */
	public List<Value> getValues() {
		return values;
	}

	/**
	 * Returns the list of persistent values.
	 *
	 * @return The list of persisten values.
	 */
	public List<Value> getPersistentValues() {
		List<KeyPointer> persistentKeyPointers = columns.getPersistentKeyPointers();
		List<Value> persistentValues = new ArrayList<>(persistentKeyPointers.size());
		for (KeyPointer pointer : persistentKeyPointers) {
			persistentValues.add(values.get(pointer.getIndex()));
		}
		return persistentValues;
	}

	/**
	 * Returns the list of persistent key pointers.
	 *
	 * @return the list of persistent key pointers.
	 */
	public List<KeyPointer> getPersistentKeyPointers() {
		return columns.getPersistentKeyPointers();
	}

	/**
	 * Set the list of values.
	 *
	 * @param values The list of values.
	 */
	public void setValues(List<Value> values) {
		this.values = values;
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param index The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(int index, Value value) {
		values.set(index, value);
		values.get(index).setModified(true);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param index The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(int index, BigDecimal value) {
		values.get(index).setBigDecimal(value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param index The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(int index, boolean value) {
		values.get(index).setBoolean(value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param index The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(int index, byte[] value) {
		values.get(index).setByteArray(value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param index The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(int index, ByteArray value) {
		values.get(index).setByteArray(value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param index The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(int index, Date value) {
		values.get(index).setDate(value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param index The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(int index, Time value) {
		values.get(index).setTime(value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param index The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(int index, Timestamp value) {
		values.get(index).setTimestamp(value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param index The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(int index, String value) {
		values.get(index).setString(value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param index The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(int index, double value) {
		values.get(index).setDouble(value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param index The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(int index, int value) {
		values.get(index).setInt(value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param index The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(int index, long value) {
		values.get(index).setLong(value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param alias The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(String alias, Value value) {
		int index = columns.getFieldIndex(alias);
		setValue(index, value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param alias The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(String alias, BigDecimal value) {
		int index = columns.getFieldIndex(alias);
		setValue(index, value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param alias The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(String alias, boolean value) {
		int index = columns.getFieldIndex(alias);
		setValue(index, value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param alias The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(String alias, byte[] value) {
		int index = columns.getFieldIndex(alias);
		setValue(index, value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param alias The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(String alias, ByteArray value) {
		int index = columns.getFieldIndex(alias);
		setValue(index, value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param alias The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(String alias, Date value) {
		int index = columns.getFieldIndex(alias);
		setValue(index, value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param alias The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(String alias, Time value) {
		int index = columns.getFieldIndex(alias);
		setValue(index, value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param alias The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(String alias, Timestamp value) {
		int index = columns.getFieldIndex(alias);
		setValue(index, value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param alias The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(String alias, String value) {
		int index = columns.getFieldIndex(alias);
		setValue(index, value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param alias The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(String alias, double value) {
		int index = columns.getFieldIndex(alias);
		setValue(index, value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param alias The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(String alias, int value) {
		int index = columns.getFieldIndex(alias);
		setValue(index, value);
	}

	/**
	 * Set the value at the given index.
	 *
	 * @param alias The index of the value.
	 * @param value The value to set.
	 */
	public void setValue(String alias, long value) {
		int index = columns.getFieldIndex(alias);
		setValue(index, value);
	}

	/**
	 * Get the primary key pointers.
	 *
	 * @return The primary key pointers list.
	 */
	public List<KeyPointer> getPrimaryKeyPointers() {
		return columns.getPrimaryKeyPointers();
	}

	/**
	 * Get an index key from an array of key pointers.
	 *
	 * @param pointers A list of KeyPointer's
	 * @return An OrderKey
	 */
	public OrderKey getIndexKey(List<KeyPointer> pointers) {
		if (pointers != null) {
			OrderKey orderKey = new OrderKey(pointers.size());
			for (KeyPointer pointer : pointers) {
				int index = pointer.getIndex();
				boolean asc = pointer.isAsc();
				orderKey.add(getValue(index), asc);
			}
			return orderKey;
		}
		return null;
	}

	/**
	 * Get the primary key.
	 *
	 * @return An <code>IndexKey</code>
	 */
	public OrderKey getPrimaryKey() {
		return getIndexKey(getPrimaryKeyPointers());
	}

	/**
	 * Check if the record has been modified.
	 *
	 * @return A boolean.
	 */
	public boolean isModified() {
		for (Value value : values) {
			if (value.isModified()) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Returns a negative integer, zero, or a positive integer as this record is less than, equal to, or greater than
	 * the specified value.
	 *
	 * @param o The object to compare.
	 * @return The compartion integer.
	 */
	@Override
	public int compareTo(Object o) {
		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
	}

}
